Skip to content

Use the .drectve section for exporting symbols from dlls on Windows #142568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bjorn3
Copy link
Member

@bjorn3 bjorn3 commented Jun 16, 2025

While it would be reasonable to expect the Windows linker to handle linker args in the .drectve section identical to cli arguments, as it turns out exporting weak symbols only works when the /EXPORT is in the .drectve section, not when it is a linker argument or when a .DEF file is used.

Necessary for and extracted out of #142366.

Thanks @dpaoliello for figuring out this weird quirk of link.exe!

@rustbot
Copy link
Collaborator

rustbot commented Jun 16, 2025

r? @wesleywiser

rustbot has assigned @wesleywiser.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot
Copy link
Collaborator

rustbot commented Jun 16, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 16, 2025
@bjorn3 bjorn3 force-pushed the windows_symbols_o_export branch from e63c76b to 94ec88c Compare June 16, 2025 09:25
@bjorn3
Copy link
Member Author

bjorn3 commented Jun 16, 2025

@bors2 try jobs=x86_64-msvc-1,x86_64-msvc-2

@rust-bors
Copy link

rust-bors bot commented Jun 16, 2025

⌛ Trying commit 5e2efb6 with merge 7bae343

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jun 16, 2025
Use the .drectve section for exporting symbols from dlls on Windows

While it would be reasonable to expect the Windows linker to handle linker args in the .drectve section identical to cli arguments, as it turns out exporting weak symbols only works when the /EXPORT is in the .drectve section, not when it is a linker argument or when a .DEF file is used.

Necessary for and extracted out of #142366.

Thanks `@dpaoliello` for figuring out this weird quirk of link.exe!
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
@rust-bors
Copy link

rust-bors bot commented Jun 16, 2025

💔 Test failed

@bjorn3
Copy link
Member Author

bjorn3 commented Jun 16, 2025

@bors2 try jobs=x86_64-msvc-1,x86_64-msvc-2

@rust-bors
Copy link

rust-bors bot commented Jun 16, 2025

⌛ Trying commit c353c7d with merge 3dadabc

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jun 16, 2025
Use the .drectve section for exporting symbols from dlls on Windows

While it would be reasonable to expect the Windows linker to handle linker args in the .drectve section identical to cli arguments, as it turns out exporting weak symbols only works when the /EXPORT is in the .drectve section, not when it is a linker argument or when a .DEF file is used.

Necessary for and extracted out of #142366.

Thanks `@dpaoliello` for figuring out this weird quirk of link.exe!
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
Copy link
Contributor

@dpaoliello dpaoliello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

One overall design question: is it supported for folks to run the linker themselves AND specify their own def file? A couple of my changes broke the Chromium and Fuschia folks, so I'm a little paranoid now about changing anything to do with linking that may affect them.

@@ -1116,10 +1122,6 @@ impl<'a> Linker for MsvcLinker<'a> {
// straight to exports.
writeln!(f, "LIBRARY")?;
writeln!(f, "EXPORTS")?;
for symbol in symbols {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe that we need the def file any more, we should be able to get away with passing /DLL to the linker.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the mingw docs I read something about the presence of a .DEF file disabling auto-export of certain symbols. I don't know if the same applies to MSVC, so I added it here just to be safe.

@bjorn3
Copy link
Member Author

bjorn3 commented Jun 16, 2025

One overall design question: is it supported for folks to run the linker themselves AND specify their own def file?

This PR on it's own has no effect at all on people running the linker themself. Both the .DEF file and the symbols.o file are only generated when rustc itself runs the linker.

As for a future PR that will use weak definitions, those symbols are not meant to be exported from a staticlib anyway. The only case that could break is if someone (Chromium) tries to link rlibs together into a dll and wants to export all symbols, which we don't officially support anyway. The Chromium people have had to adaot their build system for rustc changes anyway and I expect it to not all that hard to adapt to said future weak symbol usage too.

@rust-bors
Copy link

rust-bors bot commented Jun 16, 2025

💔 Test failed

@dpaoliello
Copy link
Contributor

💔 Test failed

The test executable failed with 0xc0000135 which is STATUS_DLL_NOT_FOUND

Looks like this is a test for shaking out unused functions: is the compiler supposed to do that, or is it relying on the linker?

@bjorn3
Copy link
Member Author

bjorn3 commented Jun 17, 2025

@bors2 try jobs=x86_64-msvc-1,x86_64-msvc-2

@rust-bors
Copy link

rust-bors bot commented Jun 17, 2025

⌛ Trying commit 8c88ff4 with merge e97fbdf

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jun 17, 2025
Use the .drectve section for exporting symbols from dlls on Windows

While it would be reasonable to expect the Windows linker to handle linker args in the .drectve section identical to cli arguments, as it turns out exporting weak symbols only works when the /EXPORT is in the .drectve section, not when it is a linker argument or when a .DEF file is used.

Necessary for and extracted out of #142366.

Thanks `@dpaoliello` for figuring out this weird quirk of link.exe!
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
@bjorn3 bjorn3 force-pushed the windows_symbols_o_export branch from 8c88ff4 to fc88eae Compare June 17, 2025 09:01
@rust-bors
Copy link

rust-bors bot commented Jun 17, 2025

☀️ Try build successful (CI)
Build commit: e97fbdf (e97fbdf63e618534ac0929eef0229de9fb477dad, parent: 55d436467c351b56253deeba209ae2553d1c243f)

While it would be reasonable to expect the Windows linker to handle
linker args in the .drectve section identical to cli arguments, as it
turns out exporting weak symbols only works when the /EXPORT is in the
.drectve section, not when it is a linker argument or when a .DEF file
is used.
@bjorn3 bjorn3 force-pushed the windows_symbols_o_export branch from fc88eae to e6aa201 Compare June 17, 2025 11:41
@bjorn3
Copy link
Member Author

bjorn3 commented Jun 17, 2025

Squashed everything and reverted some now unnecessary changes.

@rustbot ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants